/*
 * alarm.js
 * 报警统计
 */
;
(function (call) {
	var main = {
		baseUrl: CafeJS.CXT,
		mapId: "tjhyMap",
		init: function () {
			this.initView();
			this.bindEvt();
		},
		initView: function () {
			var time = $("#datepicker .datepicker-date span").text()
			time = new Date(time);
			this.endTime = CafeJS.Datepicker({
				appendTo: "#endAlarmTime",
				type: 1,
				dateFmt: "yyyy-MM-dd hh:mm",
				curDate: time
			});

			time.setHours(time.getHours() - 3);
			this.startTime = CafeJS.Datepicker({
				appendTo: "#startAlarmTime",
				type: 1,
				dateFmt: "yyyy-MM-dd hh:mm",
				curDate: time
			});
		},
		bindEvt: function () {
			var that = this;
			$("#modal_alarm .close").click(function () {
				$("#modal_alarm").remove();
				$("#alarm").removeClass("active");
			});
			$('.condition-item .submit').click(function () {
				$(".alarm-list table tbody").empty();

				that.buildTableHeader();
				if (!that.getParams()) return;
				loading.add('#modal_alarm');
				$.getJSON(api.internal.business.queryHisWarn, that.getParams(), function (res) {
					loading.remove();
					if (res.isSuccess || res.data) {
						that.bulidHistoryAlarm(res.data);
					};
				})
			});
			$(".condition-item .export").click(function () {
				$.exportExcel({
					tableNm: '报警统计',
					tableId: 'alarmTable'
				})
			})
			$("#warnType").change(function () {
				$(".alarm-list table tbody").empty();
				that.buildTableHeader();
			})
			$(".condition-item .submit").trigger("click");
		},
		getParams: function () {
			var startTime = this.startTime.getVal() + ":00";
			var endTime = this.endTime.getVal() + ":00";
			d1 = new Date(startTime.replace(/\-/g, "\/"));
			d2 = new Date(endTime.replace(/\-/g, "\/"));
			var time = 1 * 24 * 60 * 60 * 1000
			var area = $("#areaSelect").val();
			var siteType = $("#siteType").val();
			var warnLevel = $("#warnLevel").val();
			var warnType = $("#warnType").val();
			var params = {
				startTime: startTime,
				endTime: endTime,
				areaCode: area,
				stationTypes: siteType,
				warnLevel: warnLevel,
				element: warnType
			}
			if (!area) delete params.areaCode;
			if (warnType == 'rain') {
				delete params.endTime;
			} else if ((d2 - d1) > time) {
				params = ''
				alert('开始时间和结束时间间隔不能超过1天,请重新设置查询时间')
				return;
			}
			return params;
		},
		// 构建表头
		buildTableHeader: function () {
			var warnType = $("#warnType").val();
			var $warnLevel = $("#warnLevel")
			if (warnType == 't' || warnType == 'vis') {
				$warnLevel.find('option:eq(1)').attr('selected', 'selected');
				$warnLevel.find('option:eq(0)').hide();
			} else {
				$warnLevel.find('option:eq(0)').show();
				$warnLevel.find('option:eq(0)').attr('selected', 'selected');
				$warnLevel.find('option:eq(1)').removeAttr('selected', 'selected');
			}
			if (warnType == 'rain') {
				$(".alarm-endtime").parent().hide();
				$(".alarm-starttime").siblings('label').text('时间：');
			} else {
				$(".alarm-endtime").parent().show();
				$(".alarm-starttime").siblings('label').text('开始时间：');
			}
			var $thead = $(".alarm-list table thead tr").empty();
			var $th = '<th style="width: 5%">序号</th><th>发布时间</th><th>站点名称</th><th>站号</th>'
			$th += '<th>所属区域</th><th>报警等级</th><th>报警类型</th>'
			if (warnType == 'rain') {
				$th += '<th>1h降水</th><th>3h降水</th><th>6h降水</th><th>12h降水</th>'
			} else if (warnType == 't') {
				$th += '<th>温度</th>'
			} else if (warnType == 'vis') {
				$th += '<th>能见度</th>'
			} else {
				$th += '<th>风速</th>'
			}
			$thead.append($th);
			$(".alarm-list table thead tr th").slice(1).css('width', 95 / 7 + "%");
			if (warnType == 'rain') {
				$(".alarm-list table thead tr th").slice(1).css('width', 95 / 10 + "%");
			}
		},
		// 构建表内容
		bulidHistoryAlarm: function (data) {
			var warnType = $("#warnType").val();
			var content = "";
			var columnW = 95 / 7;
			if (warnType == 'rain') {
				columnW = 95 / 10;
			}
			if (warnType == 'vis') {
				data.sort(function (i, j) { return i.val - j.val });
			} else {
				data.sort(function (i, j) { return j.val - i.val });
			}
			data.forEach(function (item, index) {
				var number = ++index
				content += '<tr class="alarm-item">'
				if (warnType == 'rain') {
					content += '<td style="width: 5%">' + number + '</td>'
				} else {
					content += '<td style="width: 5%">' + number + '</td>'
				}
				content += '<td title="' + item.time + '" style="width: ' + columnW + '%">' + item.time.substr(0, 16) + '</td>'
				content += '<td style="width: ' + columnW + '%">' + item.stationName + '</td>'
				content += '<td style="width: ' + columnW + '%">' + item.stationId + '</td>'
				content += '<td style="width: ' + columnW + '%">' + (item.areaType || "&nbsp;") + '</td>'
				content += '<td title="' + item.warnName + '" style="width: ' + columnW + '%">' + item.warnName + '</td>'
				content += '<td title="' + item.warnType + '" style="width: ' + columnW + '%">' + item.warnType + '</td>'
				if (warnType == 'rain') {
					content += '<td style="width: ' + columnW + '%">' + (item.val ? Number(item.val).toFixed(1) : '&nbsp') + '</td>'
					content += '<td style="width: ' + columnW + '%">' + (item.val2 ? Number(item.val2).toFixed(1) : '&nbsp') + '</td>'
					content += '<td style="width: ' + columnW + '%">' + (item.val3 ? Number(item.val3).toFixed(1) : '&nbsp') + '</td>'
					content += '<td style="width: ' + columnW + '%">' + (item.val4 ? Number(item.val4).toFixed(1) : '&nbsp') + '</td>'
				} else {
					content += '<td style="width: ' + columnW + '%">' + (item.val || '&nbsp') + '</td>'
				}
				content += '</tr>'
			})
			$(".alarm-list table tbody").append(content);
		},
	};

	if (typeof call === 'function') {
		call(main);
	}

})(function (main) {
	CafeJS.models(['datepicker'], function () {
		main.init();
	});
});